import { _decorator, Component, Node, director, find, Tween, Vec3, CCString } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('GlobalClickEvent')
export class GlobalClickEvent extends Component {

    @property(CCString) clickId: string = ''
    myTween: Tween<Node> = new Tween()
    smallScale: Vec3 = new Vec3(0, 0, 0)
    defaultScale: Vec3 = new Vec3(0, 0, 0)
    clickStart: boolean = false

    start() {
        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this)
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this)
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this)
    }

    update(deltaTime: number) {

    }

    onTouchStart() {
        this.clickStart = true
    }

    onTouchEnd() {
        if (!this.clickStart) {
            return
        }
        switch (this.clickId) {
            case '详情':
                this.clickDetail()
                break;
            case '取消详情':
                this.clickCancelDetail()
                break;
            case '排行榜':
                this.clickRank()
                break;
            case '取消排行榜':
                this.clickCancelRank()
                break;
            case '菜单':
                this.clickMeum()
                break;
            case '取消菜单':
                this.clickCancelMeum()
                break;
            case '关闭':
                this.clickQuit()
                this.clickCancelMeum()
                break;

            default:
                console.log('未知clickId', this.clickId);
                break;
        }

        this.clickStart = false
    }

    clickDetail() {
        const UIStartBtn: Node = find('UIRoot/UIStart/开始游戏')
        const UIDetail: Node = find('UIRoot/UIDetail')
        this.smallScale.set(0.7, 0.7, 0.7)
        this.defaultScale.set(1, 1, 1)
        UIDetail.setScale(this.smallScale)
        UIStartBtn.active = false
        UIDetail.active = true
        this.myTween.target(UIDetail).to(0.1, { scale: this.defaultScale }).start()
    }

    clickCancelDetail() {
        const UIStartBtn: Node = find('UIRoot/UIStart/开始游戏')
        const UIDetail: Node = find('UIRoot/UIDetail')
        this.smallScale.set(0.7, 0.7, 0.7)
        this.defaultScale.set(1, 1, 1)
        UIDetail.setScale(this.defaultScale)
        this.myTween.target(UIDetail).to(0.1, { scale: this.smallScale }, {
            onComplete: () => {
                UIStartBtn.active = true
                UIDetail.active = false
            }
        }).start()
    }

    clickRank() {
        const UIStartBtn: Node = find('UIRoot/UIStart/开始游戏')
        const UIRank: Node = find('UIRoot/UIRank')
        this.smallScale.set(0.7, 0.7, 0.7)
        this.defaultScale.set(1, 1, 1)
        UIRank.setScale(this.smallScale)
        UIStartBtn.active = false
        UIRank.active = true
        this.myTween.target(UIRank).to(0.1, { scale: this.defaultScale }).start()
    }

    clickCancelRank() {
        const UIStartBtn: Node = find('UIRoot/UIStart/开始游戏')
        const UIRank: Node = find('UIRoot/UIRank')
        this.smallScale.set(0.7, 0.7, 0.7)
        this.defaultScale.set(1, 1, 1)
        UIRank.setScale(this.defaultScale)
        this.myTween.target(UIRank).to(0.1, { scale: this.smallScale }, {
            onComplete: () => {
                UIStartBtn.active = true
                UIRank.active = false
            }
        }).start()
    }

    clickMeum() {
        const UIGameFightMove: Node = find('UIRoot/UIGame/fightMove')
        const UIGameFightRotate: Node = find('UIRoot/UIGame/fightRotate')
        const UIGameUpDownStream: Node = find('UIRoot/UIGame/upDownStream')
        const UIMeum: Node = find('UIRoot/UIMeum')
        this.smallScale.set(0.7, 0.7, 0.7)
        this.defaultScale.set(1, 1, 1)
        UIMeum.setScale(this.smallScale)
        UIGameFightMove.active = false
        UIGameFightRotate.active = false
        UIGameUpDownStream.active = false
        UIMeum.active = true
        this.myTween.target(UIMeum).to(0.1, { scale: this.defaultScale }).start()
    }

    clickCancelMeum() {
        const UIGameFightMove: Node = find('UIRoot/UIGame/fightMove')
        const UIGameFightRotate: Node = find('UIRoot/UIGame/fightRotate')
        const UIGameUpDownStream: Node = find('UIRoot/UIGame/upDownStream')
        const UIMeum: Node = find('UIRoot/UIMeum')
        this.smallScale.set(0.7, 0.7, 0.7)
        this.defaultScale.set(1, 1, 1)
        UIMeum.setScale(this.defaultScale)
        this.myTween.target(UIMeum).to(0.1, { scale: this.smallScale }, {
            onComplete: () => {
                UIGameFightMove.active = true
                UIGameFightRotate.active = true
                UIGameUpDownStream.active = true
                UIMeum.active = false
            }
        }).start()
    }

    clickQuit(){
        director.loadScene("start")
    }
}


